package com.countryside.controller;

import com.countryside.pojo.*;

import com.countryside.pojo.query.CompanyWorkQuery;
import com.countryside.service.CompanyWorkService;
import com.countryside.service.RegionService;
import com.countryside.service.WorkCategoryService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
public class CompanyWorkController {
    @Autowired
    CompanyWorkService companyWorkService;
    @Autowired
    WorkCategoryService workCategoryService;
    @Autowired
    RegionService regionService;

    /**
     * 显示所有招工信息
     * @param model
     * @param companyWorkQuery
     * @return
     */
    @GetMapping("/findwork")
    public String findWork(Model model, CompanyWorkQuery companyWorkQuery){
        PageInfo<CompanyWork> companyWorkPageInfo = companyWorkService.listCompanyWork(companyWorkQuery);
        model.addAttribute("page", companyWorkPageInfo);
        //点击全部分类 遍历出所有工作大类
        List<WorkCategory> workCategories = workCategoryService.getCategory("00");
        model.addAttribute("workCategories", workCategories);
        //遍历出全国地区
        List<Region> regions = regionService.getRegion("000");
        model.addAttribute("regions", regions);
        return "findwork";
    }


    /**
     * 招工信息分类
     * @param id
     * @param companyWorkQuery
     * @param model
     * @return
     */
    @GetMapping("/findwork/{id}")
    public String getSonKind(@PathVariable("id") String id, //id为经过前端拼接而成
                             CompanyWorkQuery companyWorkQuery,
                             Model model) {
        //遍历出所有工种
        List<WorkCategory> workCategories = workCategoryService.getCategory("00");
        model.addAttribute("workCategories", workCategories);
        //遍历出所有省份
        List<Region> regions = regionService.getRegion("000");
        model.addAttribute("regions", regions);
        //初始化种类和地区
        WorkCategory category = null;
        Region region = null;
        //当拼接码中含有w时，相当于选中了一个工种
        if (id.contains("w")) {//03w-14s-03p-14c-
            //获取该工种的编码
            String wId = id.substring(id.indexOf("w") - 2, id.indexOf("w"));
            //通过工种编号遍历出详细工种 并放到request域 wId作为详细工种的父标签id
            List<WorkCategory> sonWorkCategories = workCategoryService.getCategory(wId);
            model.addAttribute("sonWorkCategories", sonWorkCategories);
            //获取父标签的类别名称
            category = workCategoryService.getACategory(wId);
            model.addAttribute("category", category.getKind());
        }
        //当拼接码中含有p时，相当于选中一个省份
        if (id.contains("p")) {
            //获取省份编码
            String pId = id.substring(id.indexOf("p") - 3, id.indexOf("p"));
            //通过省份编码遍历出所有城市
            List<Region> sonRegions = regionService.getRegion(pId);
            model.addAttribute("sonRegions", sonRegions);
            //通过省份编码获取该省份，并且将该省份名称放到request域
            region = regionService.getARegion(pId);
            model.addAttribute("region", region.getPlace());
        }
        if (id.contains("s")) {
            String sId = id.substring(id.indexOf("s") - 2, id.indexOf("s"));
            //通过详细工种编码获取详细工种
            category = workCategoryService.getACategory(sId);
            model.addAttribute("category", category.getKind());
        }
        if (id.contains("c")) {
            String cId = id.substring(id.indexOf("c") - 3, id.indexOf("c"));
            //通过城市编码获取该城市
            region = regionService.getARegion(cId);
            model.addAttribute("region", region.getPlace());
        }
        if (category == null && region !=null) {
            //若只对地区进行了筛选，则遍历出该地区的招工信息
            companyWorkQuery.setAddress(region.getPlace());
            PageInfo<CompanyWork> companyWorkPageInfo = companyWorkService.listCompanyWork(companyWorkQuery);
            model.addAttribute("page", companyWorkPageInfo);
        } else if (category != null && region == null) {
            //若只对职位进行了筛选，则遍历出该职位的招工信息
            companyWorkQuery.setTrade(category.getKind());
            PageInfo<CompanyWork> companyWorkPageInfo = companyWorkService.listCompanyWork(companyWorkQuery);
            model.addAttribute("page", companyWorkPageInfo);
        } else {
            //否则就通过进行职位和地区筛选
            companyWorkQuery.setAddress(region.getPlace());
            companyWorkQuery.setTrade(category.getKind());
            PageInfo<CompanyWork> companyWorkPageInfo = companyWorkService.listCompanyWork(companyWorkQuery);
            model.addAttribute("page", companyWorkPageInfo);
        }
        return "findwork";
    }

    /**
     * 跳转到发布招工信息页面
     * @return
     */
    @RequestMapping("/toCompanyWorkPublish")
    public String employeeInfo(){
        return "companyWorkPublish";
    }

    /**
     * 修改招工信息状态
     * @param id
     * @return
     */
    @GetMapping("/updateState0/{id}")
    public String updateState0(@PathVariable("id") String id) {
        companyWorkService.updateState(id,1);
        return "redirect:/user/getEmployeeWork";
    }
    /**
     * 修改招工信息状态
     * @param id
     * @return
     */
    @GetMapping("/updateState1/{id}")
    public String updateState1(@PathVariable("id") String id) {
        companyWorkService.updateState(id,0);
        return "redirect:/user/getEmployeeWork";
    }

    /**
     * 删除招工信息
     * @param id
     * @return
     */
    @GetMapping("/deleteCompanyWork/{id}")
    public String deteleCompanyWork(@PathVariable("id") String id) {
        companyWorkService.deleteCompanyWork(id);
        return "redirect:/user/getEmployeeWork";
    }

    /**
     * 添加招工信息
     * @param session
     * @param companyWork
     * @param request
     * @param model
     * @return
     */
    @PostMapping("/addCompanyWork")
    public String companyWorkPublish(HttpSession session, CompanyWork companyWork,
                                     HttpServletRequest request, Model model){
        User user = (User) session.getAttribute("user");
        String cwId = UUID.randomUUID().toString();
        Company company = companyWorkService.queryCompanyByUserId(user.getId());
        String address =request.getParameter("province") +
                request.getParameter("city") +
                request.getParameter("region") +
                request.getParameter("detailAddress");
        companyWork.setId(cwId);
        companyWork.setAddress(address);
        companyWork.setCompanyId(company.getId());
        companyWork.setUserId(user.getId());
        companyWorkService.companyWorkPublish(companyWork);
        session.setAttribute("cwId", cwId);
        return "redirect:/user/getEmployeeWork";
    }

    /**
     * 进入招工信息详细页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/toBoss/{id}")
    public String toBoss(@PathVariable("id")String id,Model model){
        CompanyWork work = companyWorkService.queryCompanyByCompanyId(id);
        model.addAttribute("work",work);
        List<CompanyWork> companyWorks = companyWorkService.queryWorkByAddress(work.getAddress().substring(0, 2));
        model.addAttribute("recommend",companyWorks);
        return "boss";
    }



}
